from django.shortcuts import render
from django.views import generic
from django.shortcuts import redirect

from . import forms
from . import utils


class CouponApplication(generic.View):

    """ Обработчик отвечающий за купоны """

    def post(self, request):

        bound_form = forms.CouponApplyForm(request.POST)

        if bound_form.is_valid():

            code = bound_form.cleaned_data['code']
            user_coupon = utils.Coupon(request)
            user_coupon.add(code)
            return redirect('cart:display_cart')
